package iotwisp.school.api.controller.pc_admin.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import iotwisp.base.cqrs.BusinessException;
import iotwisp.base.orm.query.pagination.Pagination;
import iotwisp.base.rest.JSONResultDTO;
import iotwisp.base.rest.RestBaseController;
import iotwisp.base.util.ValidationUtils;
import iotwisp.school.system.entity.County;
import iotwisp.school.system.qo.CountyQO;
import iotwisp.school.system.service.CountyService;

@RestController
@RequestMapping("/api/system/county")
public class CountyController extends RestBaseController {

	@Autowired
	private CountyService countyService;

	@PostMapping("/query")
	public ResponseEntity<JSONResultDTO> queryCounty(@RequestBody CountyQO qo) throws BusinessException {

		ValidationUtils.validate(qo);

		switch (qo.getResultType()) {
		case CountyQO.RESULT_TYPE_COUNT:
			Integer count = countyService.queryCount(qo);
			return ResponseEntity.ok(createSuccessResult(count));
		case CountyQO.RESULT_TYPE_UNIQUE:
			County county = countyService.queryUnique(qo);
			return ResponseEntity.ok(createSuccessResult(county));
		case CountyQO.RESULT_TYPE_LIST:
			List<County> countys = countyService.queryList(qo);
			return ResponseEntity.ok(createSuccessResult(countys));
		case CountyQO.RESULT_TYPE_PAGINATION:
			Pagination pagination = countyService.queryPagination(qo);
			return ResponseEntity.ok(createSuccessResult(pagination));
		default:
			return ResponseEntity.badRequest()
					.body(createFailResult(BusinessException.NECESSARY_PARAM_NULL, "resultType值有误", null));
		}

	}

}
